$(document).ready(function () {
    $("#button_question").click(function () {
        $.ajax({
            type: "get",
            url: "/exam/getAllQuestion",
            dataType: "json",
            success: function (data) {
                var table = $("#t1");
                for (var i in data) {
                    table.append("<tr><td>" + data[i].id + "</td><td>" + data[i].content + "</td><td><input type='button' value='选择' onclick='details(this) '/></td></tr>");
                }
            }

        })
    })
    $("#button_join").click(function () {
        var content = $("#tquestion").val();
        if (content == null || content == "") {
            alert("内容不能为空！")
        } else {
            $.ajax({
                type: "get",
                url: "/exam/addQuestion",
                data: {
                    content: content
                },
                dataType: "json",
                success: function (data) {
                    alert(data.content);
                    var table = $("#tques");
                    table.append("<tr><td>" + data.id + "</td><td>" + data.content + "</td><td><input type='button' value='删除' onclick='DelRowNew(this) '/></td></tr>");
                }

            })
        }
    })

    $("#submit").click(function () {
        var str = "";
        var date = $("#tdate").val();
        var tableRows = $("#tques").find("tr");

        $.ajax({
            type: "post",
            url: "/main/getNameBySession",
            dataType: "text",
            success: function (data) {
                $.ajax({
                    type: "get",
                    url: "/main/getUserByName",
                    data: {
                        name: data,
                    },
                    dataType: "json",
                    success: function (json) {
                        $.ajax({
                            type: "get",
                            url: "/exam/teamExam/checkDate/" + date,
                            async: false,
                            dataType: "text",
                            success: function (res) {
                                if (res == "true") {
                                    alert("该日期已发布过结对作业！请更换日期");
                                } else {
                                    if (tableRows.length == 1) {
                                        alert("提交内容为空!!!");
                                        return;
                                    }
                                    for (var i = 1, len = tableRows.length; i < len; i++) {
                                        var id = $(tableRows[i]).find('td').eq(0).text();
                                        str += id + ",";

                                    }
                                    $.ajax({
                                        type: "get",
                                        url: "/exam/addTeamExam",
                                        data: {
                                            questionId: str,
                                            date: date,
                                            classId: json.clazzid
                                        },
                                        success: function () {
                                            alert("结对题目发布成功！")
                                        },
                                        error: function () {

                                        }
                                    })
                                }
                            },
                            error:function () {
                              alert("请选择日期！")
                            }
                        })
                    }
                })
            },
            error: function (data) {
                alert("结对题目发布失败！")
            }
        })

    })
})

function details(row) {
    var rowIndex = row.parentNode.parentNode.rowIndex;
    var table = document.getElementById("t1");
    var id = table.rows[rowIndex].cells[0].innerHTML;
    var content = table.rows[rowIndex].cells[1].innerHTML;

    var table = document.getElementById("tques");
    var newRow = table.insertRow(1);
    var id1 = newRow.insertCell(0);
    var content1 = newRow.insertCell(1);
    var whenClick = newRow.insertCell(2);
    id1.innerHTML = id;
    content1.innerHTML = content;
    whenClick.innerHTML = "<input type='button' value='删除' onclick='DelRowNew(this)' />"
}

function DelRowNew(r) {
    var i = r.parentNode.parentNode.rowIndex;
    document.getElementById('tques').deleteRow(i);
}